package com.chenwy.example

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import org.apache.commons.lang3.StringUtils

class Test1 {

    static def makeAct(def act) {
        String minusStr = 'cAct=4'
        int cActIndex = act.indexOf(minusStr)
        if (act.startsWith('http://') && cActIndex > -1) {//安卓含cAct=4的act增加browser参数
            try {
                if (act.endsWith(minusStr)) {
                    minusStr = act.substring(cActIndex - 1, cActIndex) + minusStr
                } else if (act.indexOf('&' + minusStr) > -1) {
                    minusStr = '&' + minusStr
                } else if (act.indexOf('?' + minusStr + '&') > -1) {
                    minusStr += '&'
                }
                def result = [//act字段是json格式客户端实行新策略
                              'cAct'   : 4,
                              'url'    : act - minusStr, //去掉原url中的cAct=4参数，以json格式返回
                              'browser': 'tengxun'
                ]
                return result;
            } catch (Exception e) {//出错不转换act
                return act
            }
        } else {
            return act
        }
    }

    static def makeWxMiniAct(def act){
        try{
            URI uri = URI.create(act);
            String query = uri.getQuery();
            if(StringUtils.isNotEmpty(query)){
                def paramMap = [:]
                String [] params = query.split("&");
                for(String param : params){
                    String [] keyValue = param.split("=");
                    if(keyValue.length > 0){
                        paramMap[keyValue[0]] = keyValue.length > 1 ? keyValue[1] : "";
                    }
                }
                paramMap['url'] = act.replaceAll("\\??\\s*cAct\\s*=\\s*\\d+","");
                paramMap['browser'] = 'tengxun';
                return paramMap;
            }
            return act;
        }catch(Exception e){
        }
        return act;
    }

    public static def test(){
        String url = "act=4act=1act=1act=4" - "act=4"
        return url
    }
    public static void main(String[] args) {
        System.out.println(makeAct("http://www.77tianqi.com/frame/sm/qianshi"));
        //http://www.77tianqi.com/frame/sm/qianshi?from=lm3?cAct=4
        System.out.println(makeWxMiniAct("http://www.77tianqi.com/frame/sm/qianshi?from=lm3?cAct=4"));
        //http://url.ifjing.com/MRj2ue?cAct=25&wxMiniUsername=gh_bdc64ec371d2&wxMiniPath=pages/result/result
        System.out.println(makeAct("http://url.ifjing.com/MRj2ue?cAct=25&wxMiniUsername=gh_bdc64ec371d2&wxMiniPath=pages/result/result"));
        System.out.println(makeWxMiniAct("http://url.ifjing.com/Y36nIv?cAct=4"));
        System.out.println(makeWxMiniAct("http://www.77tianqi.com/frame/sm/qianshi"));
        System.out.println("http://url.ifjing.com/MRj2ue?cAct=2&wxMiniUsername=gh_bdc64ec371d2&wxMiniPath=pages/result/result".contains("cAct=25"));
        System.out.println(test());
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree("{}");
        ObjectNode objectNode = (ObjectNode)jsonNode;
        JsonNode b = objectMapper.readTree("{\"a\":\"b\"}")
        objectNode.putPOJO("a",b.path("a"));
        System.out.println(jsonNode.toString())
    }

}
